package com.example.huoher.miniaddressbook;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddpeopleActivity extends AppCompatActivity {

    private Person person = null;
    private EditText editTextnumber;
    private EditText editTextname;

    private Context context;
    private SQLiteDatabase db;
    private MyDBOpenHelper myDBHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addpeople);

        editTextname = findViewById(R.id.editText_name);
        editTextnumber = findViewById(R.id.editText_number);
        context = AddpeopleActivity.this;
        myDBHelper = new MyDBOpenHelper(context,"my.db",null,1);
        db = myDBHelper.getWritableDatabase();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.start:
                Toast.makeText(this, "您已经在该界面！", Toast.LENGTH_SHORT).show();
                break;
            case R.id.over:
                Intent intent3 = new Intent(AddpeopleActivity.this,MainActivity.class);
                startActivity(intent3);
                AddpeopleActivity.this.finish();
                break;
            case R.id.query:
                Intent intent4 = new Intent(AddpeopleActivity.this,ContactsActivity.class);
                startActivity(intent4);
                AddpeopleActivity.this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void addPerson(View view) {
        String number = editTextnumber.getText().toString();
        String name = editTextname.getText().toString();

        Person person = new Person(name,number);

        ContentValues contentValues = new ContentValues();
        contentValues.put("name",name);
        contentValues.put("number",number);
        db.insert("person",null,contentValues);
        Toast.makeText(this, "添加成功，请查看！！！", Toast.LENGTH_SHORT).show();
    }
}
